var Modules = {
	INTERNAL : {
		dependencies: {},
		toLoad: {}
	}
};

function DepNode(name){
	this.name = name;
	this.children = [];
};

Modules.INTERNAL.head = document.getElementsByTagName('head')[0];

Modules.INTERNAL.baseElement = document.getElementsByTagName('base')[0];
if (Modules.INTERNAL.baseElement) {
	Modules.INTERNAL.head = Modules.INTERNAL.baseElement.parentNode;
}

Modules.addDependency = function(dependencyName, file, requires) {
	
	Modules.INTERNAL.dependencies[dependencyName] ={
		file: file,
		requires: requires
	};
	
}

Modules.require = function(dependencyName) {
	Modules.INTERNAL.toLoad[dependencyName] = 'notLoaded';
	var dependency = Modules.INTERNAL.dependencies[dependencyName] ;
	if(dependency.requires){
		for ( var i = 0; i < dependency.requires.length; i++) {
			var require = dependency.requires[i];
			Modules.require(require);
		}
	}
	
}


Modules.onLoad = function(func) {
	Modules.INTERNAL.checkLoop(func, 0);
}
Modules.INTERNAL.checkLoop = function(func, time) {
	setTimeout(function(){
		
		var finished = true;
		var toLoad = Modules.INTERNAL.toLoad;
		for( var dependencyName in toLoad){
			if(toLoad[dependencyName] != 'loaded'){
				
				finished = false;
				
				if(toLoad[dependencyName] == 'notLoaded'){
					if(Modules.INTERNAL.readyToLoad(dependencyName)){
						Modules.INTERNAL.load(dependencyName);
					} 
				}	
			} 
			
		}
		
		if(finished == true){
			func();
		} else {
			Modules.INTERNAL.checkLoop(func, 20);	
		}
		
	}, time);
		
};
Modules.INTERNAL.readyToLoad = function(dependencyName){
	var toLoad = Modules.INTERNAL.toLoad;
	var dependency = Modules.INTERNAL.dependencies[dependencyName] ;
	if(dependency.requires){
		for ( var i = 0; i < dependency.requires.length; i++) {
			var require = dependency.requires[i];
			if(toLoad[require] != 'loaded') return false;
		}
	}
	return true;
};


Modules.INTERNAL.load = function(dependencyName) {
	if (Modules.INTERNAL.toLoad[dependencyName]=='loading' || Modules.INTERNAL.toLoad[dependencyName]=='loaded') {
		// already loading
		return;
	}
	
	Modules.INTERNAL.toLoad[dependencyName] = 'loading';
	
	var dependency = Modules.INTERNAL.dependencies[dependencyName];
	
	script = document.createElement('script');
	
	script.async = true;
	script.type = "text/javascript";
	script.src = dependency.file;

	if (Modules.INTERNAL.baseElement) {
		alert(Modules.INTERNAL.baseElement);
		Modules.INTERNAL.head.insertBefore(script, Modules.INTERNAL.baseElement);
    } else {
    	Modules.INTERNAL.head.appendChild(script);
    }
	

	if (script.attachEvent) {
		script.attachEvent('onreadystatechange', Modules.INTERNAL.onLoad(dependencyName));
	} else {
		script.addEventListener('load', Modules.INTERNAL.onLoad(dependencyName),	false);
	}
	
	
}
Modules.INTERNAL.onLoad = function(dependencyName) {
	return function(event) {
		var target = (event.currentTarget || event.srcElement);
		if (event.type !== "load" && target.readyState != "complete" && target.readyState != "loaded" ) {
            return;
        }
		
		
		
		if (target.attachEvent) {
			target.detachEvent('onreadystatechange', this);
		} else {
			target.removeEventListener('load', this);
		}
		Modules.INTERNAL.toLoad[dependencyName] = 'loaded';
	}
}